package com.huobao.yunying.activitys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_SearchProgectList;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.SearchProgectList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class Act_Search_Progect_List extends HuobaoBaseActivity
		implements OnScrollListener, OnItemClickListener, OnClickListener {

	/********************* Intent 需传过来的值 *************************************/
	// 分类ID
	private String cate_id;
	// 搜索时的关键词
	private String keyword;
	// 渠道值
	private String genre;
	// 属性值（5:26）第一个是属性名称id，第二个是属性名称值id
	private String props;
	// 品牌名
	private String brand;
	// （min-max)
	private String price;

	/****************************************************************************/

	private Adapter_SearchProgectList adapter;
	@ViewInject(R.id.listview)
	private ListView listview;

	private TextView footViewTextView;
	private int m_CurrentPage = 1;
	private boolean isLoading = true;

	@ViewInject(R.id.tv_search)
	private TextView tv_search;
	// 筛选按钮
	@ViewInject(R.id.tv_shaixuan)
	private TextView tv_shaixuan;
	
	
	@ViewInject(R.id.tv_zonghe)
	private TextView tv_zonghe;
	@ViewInject(R.id.tv_1)
	private TextView tv_zhipi;//直批

	@ViewInject(R.id.tv_2)
	private TextView tv_liutong;//流通
	@ViewInject(R.id.tv_3)
	private TextView tv_daili;//代理
	
	private  String tempgenre;
	@Override
	protected int getContentView() {
		return R.layout.layout_search_progect_list;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		tv_search.setOnClickListener(this);
		listview.setOnScrollListener(this);
		tv_shaixuan.setOnClickListener(this);
		listview.setOnItemClickListener(this);
		
		tv_zonghe.setOnClickListener(this);
		tv_zhipi.setOnClickListener(this);
		tv_liutong.setOnClickListener(this);
		tv_daili.setOnClickListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		getIntentData();
		addFootView();
		adapter = new Adapter_SearchProgectList(context);
		adapter.setGenre(genre);
		listview.setAdapter(adapter);
		getData(true);
	}

	
	
	private void getData(boolean isShowDialog) {
		isLoading = true;
		Map<String, String> map = new HashMap<String, String>();
		map.put(HttpConst.FRIST_KEY, "search");
		if (TextUtils.isEmpty(cate_id) == false) {
			map.put("cate_id", cate_id);
		}
		if (TextUtils.isEmpty(keyword) == false) {
			map.put("keyword", keyword);
		}
		if(TextUtils.isEmpty(tempgenre) == false ){
			map.put("genre", tempgenre);
		}else{
			if (TextUtils.isEmpty(genre) == false) {
				map.put("genre", genre);
			}
		}
		if (TextUtils.isEmpty(props) == false) {
			map.put("props", props);
		}
		if (TextUtils.isEmpty(brand) == false) {
			map.put("brand", brand);
		}
		if (TextUtils.isEmpty(price) == false) {
			map.put("price", price);
		}
		map.put("page", m_CurrentPage + "");
		LogUtil.e(map.toString());
		Http.getInstant().sendRequest(context, isShowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<SearchProgectList> list = JSON.parseArray(jsonObject.getString("retval"), SearchProgectList.class);
				if (list == null || list.size() == 0) {
					isLoading = true;
					footViewTextView.setText("加载完成");
					return;
				}
				if(m_CurrentPage == 1)
					adapter.clear();
				adapter.addAll(list);
				isLoading = false;
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}
	
	public void onBackClick(View v){
		finish();
	}

	private void addFootView() {
		View footview = LayoutInflater.from(this).inflate(R.layout.item_footview, null);
		footViewTextView = (TextView) footview.findViewById(R.id.tv_message);
		listview.addFooterView(footview);
	}

	private void getIntentData() {
		Intent intent = getIntent();
		cate_id = intent.getStringExtra("cate_id");
		keyword = intent.getStringExtra("keyword");
		genre = intent.getStringExtra("genre");
		props = intent.getStringExtra("props");
		brand = intent.getStringExtra("brand");
		price = intent.getStringExtra("price");
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
			if (isLoading)
				return;
			isLoading = true;
			m_CurrentPage++;
			getData(false);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == adapter.getCount())
			return;
		SkipUtils.seeDetail(context, adapter.getItem(position).goods_id, adapter.getItem(position).default_image);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_search:
			Intent intent = new Intent(this, Act_Search.class);
			startActivity(intent);
			break;
		case R.id.tv_shaixuan:
			Intent intent1 = new Intent(context, Act_Shaixuan.class);
			intent1.putExtra("cate_id", cate_id);
			intent1.putExtra("keyword", keyword);
			intent1.putExtra("genre", genre);
			intent1.putExtra("props", props);
			intent1.putExtra("brand", brand);
			intent1.putExtra("price", price);
			startActivityForResult(intent1, 1);
			break;
		case R.id.tv_1:
			m_CurrentPage = 1;
			tempgenre = "2";
			getData(true);
			break;
		case R.id.tv_2:
			m_CurrentPage = 1;
			tempgenre = "1";
			getData(true);
			break;
		case R.id.tv_3:
			m_CurrentPage = 1;
			tempgenre = "3";
			getData(true);
			break;
		case R.id.tv_zonghe:
			m_CurrentPage = 1;
			tempgenre = null;
			getData(true);
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if (arg0 == 1 && arg1 == RESULT_OK) {
			if (arg2.getStringExtra("cate_id") != null)
				this.cate_id = arg2.getStringExtra("cate_id");
			if (arg2.getStringExtra("genre") != null)
				this.genre = arg2.getStringExtra("genre");
			if (arg2.getStringExtra("props") != null)
				this.props = arg2.getStringExtra("props");
			if (arg2.getStringExtra("brand") != null)
				this.brand = arg2.getStringExtra("brand");
			if (arg2.getStringExtra("price") != null)
				this.price = arg2.getStringExtra("price");
			m_CurrentPage = 1;
			adapter.setGenre(genre);
			getData(true);
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
}
